#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  01-source_check.sh
# Version	:  2.0
# Date		:  2024/04/15
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinsec.com.cn
# History	:
#              Version 2.0, 2024/04/15
# Function	:  稳定性测试/资源查看
# Out		:
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="稳定性测试/资源查看"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB() {
    return $TPASS
}

## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB() {
    return $TPASS
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB() {
    testcase_1
    return $TPASS
}

## TODO : 无资源占用较高进程（CPU MEM 30%以上）
testcase_1() {
    local process_info=()
    #获取并筛选进程信息,经调试awk后只能是单引号
    while read -r pid cmdline cpu mem; do
        process_info+=("$pid,$cmdline,$cpu,$mem")
    done < <(top -b -n 1 | awk 'NR>7 { if ($9 >= 30 || $10 >= 30) print $1, $12, "%CPU: " $9 ", %MEM: " $10}')
    # 判断数组是否为空
    if [[ ${#process_info[@]} -eq 0 ]]; then
        OutputRet_LTFLIB ${TPASS}
        TestRetParse_LTFLIB "No processes with CPU or memory usage over 30% found."
    else
        for item in "${process_info[@]}"; do
            Info_LLE "$item"
        done
        OutputRet_LTFLIB ${TFAIL}
        TestRetParse_LTFLIB "Processes with CPU or memory usage over 30%:"
    fi

}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
